# @FileName  : screen.py
# @Time      : 2025/3/6 07:30
# @Author    : LuZhaoHui
# @Software  : PyCharm

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PIL import Image
import io
from tool.dt import *

def screen_print(msg):
    print('%s %s' % (now_time_strf(), msg))

def setButtonShell(button, bcolor, line, lcolor):
    button.setStyleSheet(
        'QPushButton{    '
        'background-color:(%d,%d,%d);   '
        'border: %dpx solid rgb(%d,%d,%d);}'
        'QPushButton:hover {    '
        'background-color:(229,241,251);   '
        'border: 1px solid rgb(0,120,215);}'
        'QPushButton:pressed {    '
        'background-color:(229,241,251);   '
        'border: 1px solid rgb(0,120,215);}'
        % (bcolor[0], bcolor[1], bcolor[2], line, lcolor[0], lcolor[1], lcolor[2])
    )


def showBox(obj, msg, title='提示'):
    QMessageBox.about(obj, title, '     %s     ' % msg)


def showBoxYes(obj, msg, title='提示'):
    reply = QMessageBox.question(obj,
                                 title,
                                 '     %s     ' % msg,
                                 QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
    if reply == QMessageBox.Yes:
        return True
    return False


def showButton(button, file):
    qicon = QIcon(file)
    button.setText('')
    button.setIcon(qicon)


def showLabel(label, file):
    img = QImage()
    img.load(file)
    new_img = img.scaled(label.size(), Qt.KeepAspectRatio)
    label.setPixmap(QPixmap.fromImage(new_img, flags=Qt.AutoColor))


def showLabelImageExt(scrollArea, areaWidget, label, self, file=None, image=None):
    img = None
    if file is not None:
        img = QImage()
        img.load(file)
    else:
        if image is not None:
            img = QImage()
            byte_arr = io.BytesIO()
            image.save(byte_arr, format='JPEG')
            bytes_data = byte_arr.getvalue()
            img.loadFromData(bytes_data)
    if img is None:
        return
    # w1 = label.size().width()
    # h1 = label.size().height()
    # print("area %d %d , %d %d" % (w, h, w1, h1))
    if self:
        w = scrollArea.width()
        h = scrollArea.height()
        areaWidget.resize(w, h)
        new_img = img.scaled(w, h, Qt.KeepAspectRatio)
        label.setPixmap(QPixmap.fromImage(new_img, flags=Qt.AutoColor))
    else:
        label.setPixmap(QPixmap.fromImage(img, flags=Qt.AutoColor))

def showLabelImage(area, label, name, self):
    img = QImage()
    img.load(name)
    if self:
        w = area.width()
        h = area.height()
        img = img.scaled(w, h, Qt.KeepAspectRatio)
    # 显示
    label.setPixmap(QPixmap.fromImage(img))


def imageResize(img, max_width, max_height):
    if img.size[0] > max_width or img.size[1] > max_height:
        # 转换格式，等比缩小
        ratio = min(max_width / img.size[0], max_height / img.size[1])
        new_width = int(img.size[0] * ratio)
        new_height = int(img.size[1] * ratio)
        # Image.NEAREST：最近邻插值（速度最快，但质量可能较差）。
        # Image.BILINEAR：双线性插值（速度较快，质量较好）。
        # Image.BICUBIC：双三次插值（速度较慢，但质量通常最好）。
        # Image.LANCZOS：Lanczos
        img = img.resize((new_width, new_height), Image.BICUBIC)
    return img


def setComboBoxHeight(combo, h):
    combo.setStyleSheet("QAbstractItemView::item{height: %dpx;}" % h)
    combo.setView(QListView())
